package com.kun;

import com.kun.domain.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @author Nick fang
 * @date 2021/7/7 0007 - 下午 23:27
 */
public class MyApp {
    public static void main(String[] args) throws IOException {
        //访问mybatis读取student数据
        //1.定义mybatis主配置文件名称，从类路径跟开始
        String config = "mybatis.xml";
        //2.读取config表示的文件
        InputStream in = Resources.getResourceAsStream(config);
        //3.创建sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder bulider = new SqlSessionFactoryBuilder();
        //4.创建sqlSessionFactory对象
        SqlSessionFactory factory = bulider.build(in);
        //5.【重要】获取sqlSession对象，从sqlSessionFactory中获取sqlSession
        SqlSession sqlSession = factory.openSession();
        //6.【重要】指定要执行的SQL语句的标识（namespace.id）
        String sqlId = "com.kun.dao.StudentDao.selectStudents";
        //7.执行语句
        List<Student> studentList = sqlSession.selectList(sqlId);
        //8.输出结果
        studentList.forEach( stu ->System.out.println(stu));
        //9.关闭sqlsession对象
        sqlSession.close();;
    }
}
